﻿// MenuWindowDlg.cpp: 实现文件
//

#include "afxdialogex.h"
#include "MenuWindowDlg.h"
#include "../winapp/resource.h"
#include "CustomFolderPickerDlg.h"
#include "YunDunDefenderDlg.h"
#include "../module/InjectFramework.h"
#include <shlobj.h>
#include "../common/pch.h"
// CMenuWindowDialog 对话框

IMPLEMENT_DYNAMIC(CMenuWindowDialog, CDialogEx)

CMenuWindowDialog::CMenuWindowDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MENUWINDOW, pParent)
{
	auto d_info =  GetDriveInfosW();
	for (auto it  = d_info.begin(); it != d_info.end(); it++)
	{
		if (it->name == L"采集硬盘" || it->name == L"采集U盘") {
			m_defsav = it->letter;
			if (m_defsav.back() != L'\\') {
				// Add a backslash to the end of the path
				m_defsav += L'\\';
			}
			m_defsav += L"采集工具\\采集结果\\病毒";
		}
	}
}

CMenuWindowDialog::~CMenuWindowDialog()
{
}

void CMenuWindowDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_DULLDISKSCAN, m_ButtonDulldiskscan);
	DDX_Control(pDX, IDC_CUSTOMSCAN, m_ButtonCustomscan);
	DDX_Control(pDX, IDC_STATIC_TEST, m_test);
	DDX_Control(pDX, IDC_STATIC_SAVE, m_save);
	DDX_Control(pDX, IDC_BUTTON_SELECT, m_select);
	DDX_Control(pDX, IDC_STATIC_RESUILT, m_resuilt);
}


BEGIN_MESSAGE_MAP(CMenuWindowDialog, CDialogEx)
	ON_BN_CLICKED(IDC_DULLDISKSCAN, &CMenuWindowDialog::OnBnClickedDulldiskscan)
	ON_BN_CLICKED(IDC_CUSTOMSCAN, &CMenuWindowDialog::OnBnClickedCustomscan)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_BUTTON_SELECT, &CMenuWindowDialog::OnBnClickedButtonSelect)
END_MESSAGE_MAP()


// CMenuWindowDialog 消息处理程序

void CMenuWindowDialog::OnBnClickedDulldiskscan()
{
	// TODO: 在此添加控件通知处理程序代码
	
	CString staticText;
	m_save.GetWindowText(staticText);

	// 判断文本是否为空
	if (staticText.IsEmpty())
	{
		// CStatic控件没有值（文本为空）
		AfxMessageBox(L"请选择保存结果的路径!");
		return;
	}
	else
	{
		// CStatic控件有值（文本不为空）
	}
	auto& framewk = ProcessFramework::instance();
	if (m_pParentDlg != nullptr)
	{
		//framewk.Init();
		TCHAR szDrives[MAX_PATH] = { 0 };
		DWORD dwRet = GetLogicalDriveStrings(MAX_PATH - 1, szDrives);
		if (dwRet > 0 && dwRet <= MAX_PATH) {
			TCHAR* szSingleDrive = szDrives;
			while (*szSingleDrive) {
				framewk.AddDir(szSingleDrive);
				szSingleDrive += lstrlen(szSingleDrive) + 1;
			}
		}
		framewk.AddDir(L"end");
		//切换下一个界面
		m_pParentDlg->SwitchTab(SCANTIME);
		framewk.work();
	}
}


void CMenuWindowDialog::OnBnClickedCustomscan()
{
	CString staticText;
	m_save.GetWindowText(staticText);

	// 判断文本是否为空
	if (staticText.IsEmpty())
	{
		// CStatic控件没有值（文本为空）
		AfxMessageBox(L"请选择保存结果的路径!");
		return;
	}
	else
	{
		// CStatic控件有值（文本不为空）
	}

	CCustomFolderPickerDialog folderPickerDialog;
	if (folderPickerDialog.DoModal() == IDOK)
	{
		auto& framewk = ProcessFramework::instance();
		if (m_pParentDlg != nullptr)
		{
			//framewk.Init();
			std::vector<CString> selectedPaths = folderPickerDialog.m_selectedPaths;
			for (const auto& path : selectedPaths)
			{
				framewk.AddDir(path.GetString());
			}
			framewk.AddDir(L"end");
			//切换下一个界面
			m_pParentDlg->SwitchTab(SCANTIME);
			framewk.work();
		}
		
	}
}

void CMenuWindowDialog::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	if (m_ButtonDulldiskscan.m_hWnd && m_ButtonCustomscan.m_hWnd && m_test&& m_save&& m_select&& m_resuilt)
	{
		auto nButtonWidth = cx * 0.2;
		auto nButtonHeight = cy * 0.2;
		auto nButtonLeft = cx * 0.2;
		auto nButtonTop = cy * 0.6;

		// Set the size and position of the button
		m_ButtonDulldiskscan.SetWindowPos(NULL, nButtonLeft, nButtonTop, nButtonWidth, nButtonHeight, SWP_NOZORDER | SWP_NOACTIVATE);

		nButtonWidth = cx * 0.2;
		nButtonHeight = cy * 0.2;
		nButtonLeft = cx * 0.6;
		nButtonTop = cy * 0.6;
		m_ButtonCustomscan.SetWindowPos(NULL, nButtonLeft, nButtonTop, nButtonWidth, nButtonHeight, SWP_NOZORDER | SWP_NOACTIVATE);


		m_FontBold.CreateFont(cy * 0.1, 0, 0, 0, FW_HEAVY,
			FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
			CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS,
			_T("Arial"));

		m_test.SetFont(&m_FontBold);
		m_test.SetWindowText(L"云盾正在保护您的电脑");
		nButtonWidth = cx * 0.6;
		nButtonHeight = cy * 0.3;
		nButtonLeft = cx * 0.2;
		nButtonTop = cy * 0.1;

		m_test.SetWindowPos(NULL, nButtonLeft, nButtonTop, nButtonWidth, nButtonHeight, SWP_NOZORDER | SWP_NOACTIVATE);

		nButtonWidth = cx * 0.5;
		nButtonHeight = cy * 0.1;
		nButtonLeft = cx * 0.2;
		nButtonTop = cy * 0.85;

		m_save.SetWindowPos(NULL, nButtonLeft, nButtonTop, nButtonWidth, nButtonHeight, SWP_NOZORDER | SWP_NOACTIVATE);

		nButtonWidth = cx * 0.2;
		nButtonHeight = cy * 0.1;
		nButtonLeft = cx * 0.7;
		nButtonTop = cy * 0.85;

		m_select.SetWindowPos(NULL, nButtonLeft, nButtonTop, nButtonWidth, nButtonHeight, SWP_NOZORDER | SWP_NOACTIVATE);

		nButtonWidth = cx;
		nButtonHeight = cy * 0.1;
		nButtonLeft = 0;
		nButtonTop = cy * 0.5;

		m_resuilt.SetWindowPos(NULL, nButtonLeft, nButtonTop, nButtonWidth, nButtonHeight, SWP_NOZORDER | SWP_NOACTIVATE);
		
		if (!m_defsav.empty())
		{
			m_save.SetWindowText(m_defsav.c_str());
			m_select.ShowWindow(SW_HIDE);
		}
	}
	// TODO: 在此处添加消息处理程序代码
	// Get the size of the client area
	// Calculate the new size and position of the button
	
}


void CMenuWindowDialog::OnBnClickedButtonSelect()
{
	// TODO: 在此添加控件通知处理程序代码
	BROWSEINFO bi = { 0 };
	bi.lpszTitle = _T("请选择一个文件夹：");
	bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;

	// 调用SHBrowseForFolder函数显示文件夹选择对话框
	LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
	if (pidl != nullptr)
	{
		// 获取用户选择的文件夹路径
		TCHAR folderPath[MAX_PATH];
		if (SHGetPathFromIDList(pidl, folderPath))
		{
			m_save.SetWindowText(folderPath);
		}

		// 释放由SHBrowseForFolder函数分配的内存
		IMalloc* pMalloc = nullptr;
		if (SUCCEEDED(SHGetMalloc(&pMalloc)) && pMalloc)
		{
			pMalloc->Free(pidl);
			pMalloc->Release();
		}
	}
}

void CMenuWindowDialog::SetReuilt(DWORD dwfilenumber, DWORD allfienumber, DWORD badFilenumber)
{
	wchar_t resuilt[MAX_PATH]{ 0 };
	swprintf_s(resuilt,L"上次扫描结果:总文件：%lu 已扫描文件：%lu 威胁文件：%lu", allfienumber, dwfilenumber, badFilenumber);
	m_resuilt.SetWindowText(resuilt);
}